package kku.cs.fgl.timeline;

public class LoopAction extends AbstractAction {
	private int loop = 1;

	private int loop_count = 0;

	private AbstractAction nextLoop = null;

	public LoopAction() {
		this(1);
	}

	public LoopAction(int loop) {
		super(0);
		this.loop_count = loop;
		nextLoop = new AbstractAction(0) {
			public void action(int time) {
			}
		};
	}

	public AbstractAction getNextLoop() {
		return nextLoop;
	}

	public void action(int time) {

	}

	public void update(int time) {

		if (state == 0) {
			state = 1;
			loop = 0;
			nextLoop.setDelay_before(getDelay_before());
		}

		if (state == 1) {
			nextLoop.state = 0;
			state = 2;
		}
		if (state == 2) {
			nextLoop.update(time);

			if (nextLoop.state == 4) {
				loop++;
				if (loop_count <= 0 || loop < loop_count) {
					state = 1;
				} else {
					state = 3;
				}
			}
		}
		if (state == 3) {
			if (getNext() != null && getNext().isActive()) {
				getNext().update(time);
				if (getNext().state == 4) {
					state = 4;
				}
			} else {
				state = 4;
			}
		}
	}
}
